package cn.bluemobi.platform.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.entity.member.Identification;
import cn.bluemobi.platform.entity.member.Member;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.mapper.MemberMapper;
import cn.bluemobi.platform.mapper.MerchantMapper;
import cn.bluemobi.platform.service.MemberService;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberMapper memberMapper;

	@Autowired
	private MerchantMapper merchantMapper;

	@Override
	public Page<Map<String, Object>> findMembers(PageCondition cond) {
		Page<Map<String, Object>> page = PageUtils.startPage(cond);
		page.setList(memberMapper.findPages(cond.getMap()));
		return page;
	}

	@Override
	public Member findMemberDetail(String memberId) {
		Member member = memberMapper.selectByPrimaryKey(Long.valueOf(memberId));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> showItems = new HashMap<String, Object>();
		if (member.getRegisterDate() != null) {
			showItems.put("registerTime", sdf.format(member.getRegisterDate())
					.toString());
		}
		if (member.getLastLoginDate() != null) {
			showItems.put("lastLoginTime", sdf
					.format(member.getLastLoginDate()).toString());
		}
		member.setShowItems(showItems);
		return memberMapper.selectByPrimaryKey(Long.valueOf(memberId));
	}

	@Override
	public MapDto disableMember(String memberId, String indentifyId, User user,
			Integer type) {
		MapDto dto = new MapDto();
		Member member = memberMapper.selectByPrimaryKey(Long.valueOf(memberId));
		if (type == 1) {
			// type=1 禁用用户
			member.setStatus(1);
		} else if (type == 2) {
			// type=2 审核用户(认证)
			member.setIsIdentification(1);
			memberMapper.verifyIdentify(Long.valueOf(indentifyId), 1);
		}
		memberMapper.updateByPrimaryKeySelective(member);
		return dto;
	}

	@Override
	public MapDto enableMember(String memberId, String indentifyId, User user) {
		MapDto dto = new MapDto();
		Member member = memberMapper.selectByPrimaryKey(Long.valueOf(memberId));
		member.setStatus(0);
		memberMapper.updateByPrimaryKeySelective(member);
		return dto;
	}

	@Override
	public Identification findMemberIndentify(String memberId) {
		Identification identification = new Identification();
		if (memberMapper.findIdentifyByMemberId(Long.valueOf(memberId), 1) == null) {
			identification = memberMapper.findIdentifyByMemberId(
					Long.valueOf(memberId), 0);
		}
		return identification;
	}

	@Override
	public Double getTotalMemberWaitRebatePoints() {
		return memberMapper.getTotalMemberWaitRebatePoints();
	}

	@Override
	public Double getTotalMemberAlreadyRebatePoints() {
		return memberMapper.getTotalMemberAlreadyRebatePoints();
	}

	@Override
	public Double getTotalTodayRebateGoldPoints() {
		return memberMapper.getTotalTodayRebateGoldPoints();
	}

	@Override
	public Double getTotalTodayRebateSilverPoints() {
		return memberMapper.getTotalTodayRebateSilverPoints();
	}

	@Override
	public MapDto moditifyMember(String memberId, Double waitMemberRebatePoints, Double consumePoints, Double withdrawalsPoints, User user) {
		MapDto dto = new MapDto();
		Member member = new Member();
		member.setId(Long.valueOf(memberId));
		member.setWaitMemberRebatePoints(BigDecimal.valueOf(waitMemberRebatePoints));
		member.setConsumePoints(BigDecimal.valueOf(consumePoints));
		member.setWithdrawalsPoints(BigDecimal.valueOf(withdrawalsPoints));
		memberMapper.updateByPrimaryKeySelective(member);
		return dto;
	}
}
